﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Panny.Data;
using System.Data;
namespace Model
{
    public class CorpVideoData
    {
        public int Insert(string path, string corpID)
        {
            string sql = " insert into CorpVideo(VidioURL,CorpID) values(@Path,@CorpID)";
            using (SqlText insertion = new SqlText(sql))
            {
                insertion.AddParameter("@Path", path);
                insertion.AddParameter("@CorpID", corpID);
                return insertion.ExecuteNonQuery();
            }
        }

        public DataSet ListByCorpID(string corpID)
        {
            string sql = "select * from CorpVideo where Corp=@CorpID";
            using (SqlText selection = new SqlText(sql))
            {
                selection.AddParameter("CorpID", corpID);
                return selection.GetDataset();
            }
        }
        public string GetVideoUrlByCorpID(int cid)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("UPDATE  CorpVideo set hits = hits+1 where ");
            sql.Append("VideoID in  (select top 1 VideoID from CorpVideo where CorpID=@CorpID order by CreateOn desc) ");
            sql.Append(" select top 1 VidioURL from CorpVideo where CorpID=@CorpID order by CreateOn desc ");
            using (SqlText selection = new SqlText(sql.ToString()))
            {
                selection.AddParameter("@CorpID", cid);
                object obj = selection.ExecuteScalar();
                if (obj != null)
                {
                  return  obj.ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string GetVideoUrlByVideoID(int vid)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" UPDATE  CorpVideo set hits = hits+1 where VideoID = @VideoID ");
            sql.Append(" select VidioURL from CorpVideo where  VideoID = @VideoID");
            using (SqlText selection = new SqlText(sql.ToString()))
            {
                selection.AddParameter("@VideoID", vid);
                return selection.ExecuteScalar().ToString();
            }
        }
        public static string GetCorpIDByVideoID(int vid)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" select CorpID from CorpVideo where  VideoID = @VideoID");
            using (SqlText selection = new SqlText(sql.ToString()))
            {
                selection.AddParameter("@VideoID", vid);
                return selection.ExecuteScalar().ToString();
            }
        }


    }
}
